package top.hcode.hoj.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.mapper.CollUserStatMapper;
import top.hcode.hoj.pojo.entity.CollUserStat;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author orangej
 * @since 2025-02-10
 */
@Service
@RequiredArgsConstructor
public class CollUserStatDao extends ServiceImpl<CollUserStatMapper, CollUserStat> {

    private final  LazyDao queryDao;

    public CollUserStat getByUserIdAndCollId(String userId, String collId) {
        return lambdaQuery().eq(CollUserStat::getUserId, userId).eq(CollUserStat::getCollId, collId).one();
    }

    public boolean updateSubmitAtById(String id) {
        return lambdaUpdate().set(CollUserStat::getSubmitAt, new Date())
                .eq(CollUserStat::getId, id)
                .update();
    }

    public void saveSubjectiveScore(String collId, String userId, BigDecimal subjectiveScore) {
        CollUserStat userStat = getByUserIdAndCollId(userId, collId);
        if (userStat != null) {
            queryDao.runSql("update coll_user_stat set " +
                            "subjective_score = ?, total_score = objective_score + ? where id = ?",
                    subjectiveScore, subjectiveScore, userStat.getId());
        }
    }
}
